package com.lingz.manager.modules.scheduled;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lingz.manager.modules.wuxue.mapper.DeviceInfoMapper;
import com.lingz.manager.modules.wuxue.mapper.TuopuInsectDataMapper;
import com.lingz.manager.modules.wuxue.entity.DeviceInfo;
import com.lingz.manager.modules.wuxue.entity.TuopuInsectData;
import com.lingz.manager.modules.wuxue.utils.DeviceDataUtil;
import com.lingz.manager.modules.wuxue.utils.SendRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname TuoPuTask
 * @Description 托普云农设备监测数据任务
 * @Date 2023/12/1 15:14
 * @Created by mjh
 */
@Component
@Slf4j
public class TuoPuTask {
    @Autowired
    DeviceInfoMapper deviceInfoMapper;
    @Autowired
    TuopuInsectDataMapper tuopuInsectDataMapper;
    @Scheduled(cron = "0 0 21 * * ?")
    public void TuopuInsectDataAdd() {
        // 获取托普的所有设备
        LambdaQueryWrapper<DeviceInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeviceInfo::getFactory, "托普");
        List<DeviceInfo> deviceInfos = deviceInfoMapper.selectList(wrapper);
        //获取当前日期
        LocalDateTime today = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String start_date = today.format(formatter);
        LocalDateTime tomorrow = today.plusDays(1);
        String end_date = tomorrow.format(formatter);
        for (DeviceInfo deviceInfo : deviceInfos) {
            String deviceId = deviceInfo.getDeviceId();
            if (deviceId != null) {
                String tpUrl = DeviceDataUtil.createTPUrl("1", deviceId, start_date, end_date);
                SendRequest request = new SendRequest();
                String jsonResponse = request.sendPost(tpUrl);
                handleTPData(jsonResponse);
            }
        }
    }

    private void handleTPData(String jsonResponse) {
        JSONObject responseJson = JSONObject.parseObject(jsonResponse);
        if (responseJson != null && responseJson.containsKey("data")){
            List<TuopuInsectData> tuopuInsectDataList = new ArrayList<>();
            JSONObject data = responseJson.getJSONObject("data");
            JSONArray list = data.getJSONArray("list");
            for (int i = 0; i < list.size(); i++) {
                JSONObject jsonObject = list.getJSONObject(i);
                JSONArray imgList = jsonObject.getJSONArray("img_list");
                String deviceId = jsonObject.getString("serial_num");
                for (int j = 0; j < imgList.size(); j++) {
                    JSONObject imgListJSONObject = imgList.getJSONObject(j);
                    try {
                        TuopuInsectData tuopuInsectData = DeviceDataUtil.parseJsonToTPInsectData(imgListJSONObject, deviceId);
                        tuopuInsectDataList.add(tuopuInsectData);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            log.info(tuopuInsectDataList.toString());
            tuopuInsectDataMapper.insertBatchSomeColumn(tuopuInsectDataList);
            log.info("插入今天的托普数据(#^.^#)");
        }
    }

}
